package cn.com.cennavi.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import cn.com.cennavi.bean.DidiNewData;
import scala.annotation.meta.field;

/**
 * 对新版滴滴数据进行精简，减少存储空间
 * 
 * @ClassName: ReadDidiNewData
 * @Description: TODO（这里用一句话描述这个类的作用）
 * @author bull
 * @date 2016年7月12日 下午2:16:48
 *
 */
public class ReadDidiNewData {
	private static SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmm");
	static String inputFile = "H:\\滴滴轨迹\\20160601_110000\\110000-r-00013";
	static String outputFIleFolder = "H:\\滴滴轨迹\\20160601_110000\\minData";
	static HashMap<Integer, BufferedWriter> writeMap = new HashMap<>();;

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		createFile();
		splitDidiData(inputFile, outputFIleFolder);

	}

	private static void createFile() throws IOException {
		// TODO Auto-generated method stub

		for (int i = 0; i <= 24 * 60; i++) {
			File file = new File(outputFIleFolder + "/" + i);
			if (!file.exists()) {
				file.createNewFile();
			}
			BufferedWriter write = new BufferedWriter(new FileWriter(file, false));
			writeMap.put(i, write);
		}
	}

	/**
	 * 对滴滴数据进行精简，并按分钟存储
	 * 
	 * @throws IOException
	 */
	private static void splitDidiData(String inputFile, String outputFIleFolder) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(inputFile)));
		String line = "";
		String startTime = sf.format(new Date());
		while ((line = br.readLine()) != null) {
			 System.out.println(line);
			String[] arr = line.split("\\:");
			String carID = arr[0];
			String[] drr = arr[1].split("\\;");
			for (String dataLine : drr) {
				DidiNewData didi = DidiNewData.parseLine(dataLine);
				if (didi == null)
					continue;
				didi.carId = carID;
				int time = didi.getMin();
				BufferedWriter write = writeMap.get(time);
				if (write == null) {
					System.out.println(time);
				}
				write.write(didi.carId + "," + didi.lon + "," + didi.lat + "," + didi.timeStamp);
				write.newLine();
				write.flush();
			}

		}
		String endTime = sf.format(new Date());
		BufferedWriter write = new BufferedWriter(new FileWriter(new File(outputFIleFolder + "/" + "ok"), true));
		write.write("startTime:" + startTime + " endTime:" + endTime);
		write.newLine();
		write.flush();
		write.close();
		write = null;
	}

}
